using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace RideMe.CommandManagement
{
    public class CommandsCollection : ICollection, IEnumerable
    {
        internal CommandsCollection(CommandManager commandManager)
        {
            _commandManager = commandManager;
            commands = new SortedList();
        }

        private SortedList commands;

        private CommandManager _commandManager;

        internal CommandManager Manager
        {
            get
            {
                return _commandManager;
            }
            set
            {
                _commandManager = value;
            }
        }

        // ICollection interface
        public object SyncRoot
        {
            get
            {
                return commands.SyncRoot;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return commands.IsSynchronized;
            }
        }

        public int Count
        {
            get
            {
                return commands.Count;
            }
        }

        public void CopyTo(System.Array array, int index)
        {
            if (array != null)
            {
                this.commands.CopyTo(array, index);
            }
        }

        public void CopyTo(Command[] commands, int index)
        {
            if (commands != null)
            {
                this.commands.CopyTo(commands, index);
            }
        }

        public System.Collections.IEnumerator GetEnumerator()
        {
            return commands.GetEnumerator();
        }

        public void Add(Command command)
        {
            if (command != null)
            {
                command.Manager = Manager;
                commands.Add(command.ToString(), command);
            }
        }

        public void Remove(string tag)
        {
            commands.Remove(tag);
        }

        public bool Contains(string tag)
        {
            return commands.Contains(tag);
        }

        public bool Contains(Command command)
        {
            return commands.Contains(command);
        }

        public Command this[string tag]
        {
            get
            {
                return commands[tag] as Command;
            }
        }
    }
}
